#include "stdafx.h"
#include "comminstrument.h"


CommInstrument::CommInstrument() {
	InstrumentID			= "";
	ExchID					= "";
	ExchInstrumentID		= "";
	Index					= -1;
	HasMarketDataBestLevelSnapshot = false;
	HasMarketDataTradeSnapshot	= false;
	CanPublish				= false;

	IsMarketDataSubscribed	= false;
	MarketDataSubcriptionList.clear();
}

void CommInstrument::AddSocketToSubscribeList(CSocket * socket) {
	list<CSocket *>::const_iterator it = MarketDataSubcriptionList.cbegin();
	CSocket *sockectToCheck;
	while( it != MarketDataSubcriptionList.end() ) {
		sockectToCheck = (*it);
		if( sockectToCheck == socket ) {
			return;
		}
		++it;
	}
	MarketDataSubcriptionList.push_back(socket);
}
void CommInstrument::RemoveSocketFromSubscribeList(CSocket * socket)
{
	MarketDataSubcriptionList.remove(socket);
}


void CommInstrument::PubMarketDataBestLevelSnapshot() {
	if(HasMarketDataBestLevelSnapshot) {
		ZAdaptorMarketDataBestLevelMessage *msg = &MarketDataBestLevelSnapshot;
		list<CSocket *>::const_iterator it = MarketDataSubcriptionList.cbegin();
		CSocket *socket;
		char buf[ZAdaptorMarketDataBestLevelMessage::Length+1];
		int len;
		len = ZAdaptorMarketDataBestLevelMessage::set(buf, msg);
		while( it != MarketDataSubcriptionList.end() ) {
			socket = (CSocket *) (*it);
			socket->Send(buf, len);
			++it;
		}
	}
}

void CommInstrument::PubMarketTradeSnapshot() {
	if(HasMarketDataTradeSnapshot) {
		ZAdaptorMarketDataTradeMessage *msg = &MarketTradeSnapshot;
		list<CSocket *>::const_iterator it = MarketDataSubcriptionList.cbegin();
		CSocket *socket;
		char buf[ZAdaptorMarketDataTradeMessage::Length+1];
		int len;
		len = ZAdaptorMarketDataTradeMessage::set(buf, msg);
		while( it != MarketDataSubcriptionList.end() ) {
			socket = (CSocket *) (*it);
			socket->Send(buf, len);
			++it;
		}
	}
}














CommInstrumentList::CommInstrumentList() {
	InstrumentList.clear();
}
CommInstrument *CommInstrumentList::GetInstrumentByID(const string & id) {
	list<CommInstrument *>::const_iterator it = InstrumentList.cbegin();
	string instrID;
	CommInstrument *instr;
	int idLen = id.length();
	int len;
	while ( it != InstrumentList.end() ) {
		instr = (CommInstrument *) (*it);
		instrID = instr->InstrumentID;
		len = instrID.length();
		if( len == idLen && strncmp(instrID.c_str(), id.c_str(), len) == 0 ) {
			return instr;
		}
		++it;
	}
	return 0;
}

void CommInstrumentList::LoadAllInstrument(string & instruments) {
	int pos = instruments.find(",");
	int len;
	CommInstrument *instr;
	int index = 0;
	while(pos != string::npos) {
		instr = new CommInstrument();
		this->InstrumentList.push_back(instr);
		instr->InstrumentID = instruments.substr(0, pos);
		instr->Index = index++;
		len = instruments.length() - pos - 1;
		if( len <= 0 ) break;
		instruments = instruments.substr(pos+1, len);
		pos = instruments.find(",");
	}
	if(instruments.length() > 0) {
		instr = new CommInstrument();
		this->InstrumentList.push_back(instr);
		instr->InstrumentID = instruments;
		instr->Index = index++;
	}
}

void CommInstrumentList::RemoveMarketDataSubscriptionFromList(CSocket *socket) {
	list<CommInstrument *>::const_iterator it = InstrumentList.cbegin();
	string instrID;
	CommInstrument *instr;
	while ( it != InstrumentList.end() ) {
		instr = (CommInstrument *) (*it);
		instr->RemoveSocketFromSubscribeList(socket);
		++it;
	}
}